package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @SuppressWarnings("all")
    @Autowired
    private AttrGroupDao attrGroupDao;
    @SuppressWarnings("all")
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,String type,Long catelogId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();

        if(type.equals("base")){
            w.eq("attr_type",1);
        }else{
            w.eq("attr_type",0);
        }
        if(!catelogId.equals(0l)){
            w.eq("catelog_id",catelogId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> attrRelationByGroupId(Long attrGroupId) {
        //通过分组编号查询规格参数
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type",1);
        wrapper.eq("attr_group_id",attrGroupId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public PageUtils noattrRelation(Map<String, Object> params) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();

        w.isNull("attr_group_id");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );
        return new PageUtils(page);
    }


    @Override
    public boolean save(AttrEntity entity) {
        //添加关联属性
        //页面展示，修改状态为1
        entity.setAttrType(1);
        Long attrGroupId = entity.getAttrGroupId();
        Long catelogId = entity.getCatelogId();
        if (attrGroupId!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
            entity.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        if (catelogId!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
           entity.setCatelogName(categoryEntity.getName());
        }

        return super.save(entity);
    }
}